package com.liwentao.seckill.controller;

import com.liwentao.seckill.config.RedisConfig;
import com.liwentao.seckill.entity.vo.LoginVo;
import com.liwentao.seckill.result.R;
import com.liwentao.seckill.result.ResponseEnum;
import com.liwentao.seckill.service.IUserService;
import com.liwentao.seckill.util.MD5Util;
import com.liwentao.seckill.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Api(value = "登录管理",tags = "登录")
@Slf4j
@Controller
@RequestMapping("/seckill/login")
public class LoginController {

    @Resource
    private IUserService service;
    @Resource
    private RedisUtil redis;
    @GetMapping("/tologin")
    public String tologin(){
        return "login";
    }

    @ApiOperation(value = "dologin",notes = "登录")
    @PostMapping("/dologin")
    @ResponseBody
    public R dologin(@ApiParam(name = "loginVo",value = "登录信息",required = true)@Valid LoginVo loginVo, HttpServletRequest request, HttpServletResponse response){
        if(null==loginVo.getMobile()||null==loginVo.getPassword()){
            return R.error(ResponseEnum.LOGIN_ERROR);
        }
        loginVo.setPassword(MD5Util.inputPassToFromPass(loginVo.getPassword()));
        return service.login(loginVo,request,response);
    }
}

